package name.huzhenbo.java.patterns.command;

class RoverCommand {
    private static final RoverCommand MOVE = new RoverCommand('M');
    private static final RoverCommand TURN_LEFT = new RoverCommand('L');
    private static final RoverCommand TURN_RIGHT = new RoverCommand('R');
    private static final RoverCommand NON_EXIST = new RoverCommand('E');

    private char cmd;

    private RoverCommand(char cmd) {
        this.cmd = cmd;
    }

    void execute(Rover rover) {
        if (this == MOVE) {
            rover.getRoverState().move(rover);
        } else if (this == TURN_LEFT) {
            rover.getRoverState().turnLeft(rover);
        } else if (this == TURN_RIGHT) {
            rover.getRoverState().turnRight(rover);
        } else {
            rover.getRoverState().omit(rover);
        }
    }

    static RoverCommand valueof(char cmd) {
        if (cmd == 'M') {
            return MOVE;
        } else if (cmd == 'L') {
            return TURN_LEFT;
        } else if (cmd == 'R') {
            return TURN_RIGHT;
        } else {
            return NON_EXIST;
        }
    }
}
